<template>
  <div class="box">
    <h2>
      菜单管理页面<small
        ><el-button type="primary" size="small" round @click="showDialog()"
          >新增菜单</el-button
        ></small
      >
    </h2>
    <el-table
      :data="menus.menuList"
      style="width: 100%"
      height="350"
      row-key="id"
      dedault-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="序号" width="80"> </el-table-column>
      <el-table-column prop="pid" label="上级编号" width="80">
      </el-table-column>
      <el-table-column prop="type" label="菜单类型" width="100">
        <template v-slot="prop">
          <el-tag type="info" effect="dark" v-if="prop.row.type === 1"
            >目录</el-tag
          >
          <el-tag type="success" effect="dark" v-else>菜单</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="菜单名称" width="150">
      </el-table-column>
      <el-table-column prop="icon" label="菜单图标" width="100">
        <!-- 注释 -->
        <!-- <template v-slot="prop">
          <i :class="prop.row.icon" style="font-size: 22px"></i>
        </template> -->
      </el-table-column>
      <el-table-column prop="url" label="菜单链接"> </el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template v-slot="prop">
          <el-tag
            type="success"
            size="small"
            effect="dark"
            v-if="prop.row.status === 1"
            >启用</el-tag
          >
          <el-tag type="info" size="small" effect="dark" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="180">
        <template v-slot="props">
          <el-button
            type="success"
            size="small"
            round
            @click="editMenus(props.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="small"
            round
            @click="deleteMenus(props.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Dialog ref="VDialog"></Dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
// 导入store
import useMenuStore from "../../stores/menus.js";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
// 导入弹窗组件
import Dialog from "./dialog.vue";
const menus = useMenuStore();

const $router = useRouter();

// 获取数据
onMounted(() => {
  // 调用函数,获取菜单数据
  menus.getMenuListActions();
});
// 弹窗变量
const VDialog = ref(null);
// 打开弹窗
const showDialog = () => {
  VDialog.value.dialogVisible = true;
  // console.log(VDialog.value.dialogVisible);
};

//删除菜单
function deleteMenus(Menus) {
  // console.log("Menus", Menus);
  if (Menus.children) {
    ElMessage({
      type: "warning",
      message: "你不能删除带有子菜单的目录",
    });
    return;
  }
  ElMessageBox.confirm("确定删除此菜单吗？", "Warning", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      // 调用删除函数
      menus.delMenuListActions(Menus.id);
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "用户取消了删除此菜单",
      });
    });
}
// 编辑菜单
function editMenus(Menus) {
  // console.log("11111111111111", VDialog.value, Menus.id);
  // 显示弹窗，将数据发送给子组件
  VDialog.value.acceptValue(Menus);
  showDialog();
}
</script>

<style lang="css" scoped></style>
